package com.guoranhao.tool.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Description: gzip 相关
 * Copyright (c) Department of Research and Development/Beijing
 * All Rights Reserved.
 *
 * @version 1.0 2019年05月10日 12:02 by 宫珣（gongxun@cloud-young.com）创建
 */
public class GzipUtil {

    private static final Logger logger = LoggerFactory.getLogger(GzipUtil.class);

    /**
     * Gzip string.
     *
     * @param str the str
     * @return the string
     */
    public static String gzip(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
        } catch ( Exception e) {
            LogUtil.error(logger, e, "【gzip】error ");
        } finally {
            try {
                if (null != gzip) {
                    gzip.close();
                }
                out.close();
            } catch (IOException e) {
                LogUtil.error(logger, e, "【gzip】error ");
            }

        }
        return new String(Base64.getEncoder().encode(out.toByteArray()), StandardCharsets.UTF_8);
    }

    /**
     * Un gzip string.
     *
     * @param param the param
     * @return the string
     */
    public static String unGzip(String param) {
        if (param == null || param.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] decode = Base64.getDecoder().decode(param.getBytes());
        ByteArrayInputStream in = new ByteArrayInputStream(decode);
        GZIPInputStream ungzip = null;
        try {
            ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }

        } catch (Exception e) {
            LogUtil.error(logger, e, "【unGzip】error ");
        } finally {
            try {
                if (null != ungzip) {
                    ungzip.close();
                }
                out.close();
            } catch (IOException e) {
                LogUtil.error(logger, e, "【unGzip】error ");
            }

        }
        return new String(out.toByteArray(), StandardCharsets.UTF_8);
    }

    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args)  throws Exception {
        String gzip1 = gzip("param=123&param1=中文");
        System.out.println("gzip1 = " + gzip1);
        String unGzip1 = unGzip(gzip1);
        System.out.println("unGzip1 = " + unGzip1);
    }




}
